package com.timoon.component.panel.treetable.attachment;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.extensions.markup.html.tree.table.AbstractPropertyColumn;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation;
import org.apache.wicket.extensions.markup.html.tree.table.IRenderable;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.timoon.component.panel.checkbox.CheckBoxPanel;

/**
 * TreeTable的IColumn元素，已封装CheckBoxPanel
 * @author Administrator
 *
 */
public class CheckBoxPropertyColumn extends AbstractPropertyColumn {
	private static final long serialVersionUID = 5688971146569663437L;

	
	/**
	 * 保存用户选中的id
	 */
	private Set<String> checkedIds=new HashSet<String>();
	
	/**
	 * 
	 * @param location
	 * @param header
	 * @param propertyExpression
	 * @param checkedIds	已经选中的id
	 */
	public CheckBoxPropertyColumn(ColumnLocation location,
			String header, String propertyExpression,List<String> checkedIds) {
		super(location, header, propertyExpression);
		// TODO Auto-generated constructor stub
		
		if(checkedIds!=null){
			this.checkedIds=new HashSet<String>(checkedIds);
		}
	}

	@Override
	public IRenderable newCell(TreeNode arg0, int arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Component newCell(MarkupContainer arg0, String arg1, TreeNode arg2,
			int arg3) {
		// TODO Auto-generated method stub
		
		final Object obj=((DefaultMutableTreeNode) arg2).getUserObject();
		
		Boolean checked=false;
		if(checkedIds.size()>0){
			//如果存在初始化数据
			//获取对象里的id，结合初始化的checkIds，判断此记录是否已被选中
			try {
				String id=BeanUtils.getProperty(obj, "id");
				checked=checkedIds.contains(id);
				
			} catch (IllegalAccessException | InvocationTargetException
					| NoSuchMethodException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		Panel check=new CheckBoxPanel(arg1,new Model<Boolean>(checked)){

			@Override
			protected void onCheckUpdate(boolean newSelection) {
				// TODO Auto-generated method stub
				try {
					if(newSelection){
						checkedIds.add(BeanUtils.getProperty(obj, "id").toString());
					}else{
						checkedIds.remove(BeanUtils.getProperty(obj, "id").toString());
					}
				} catch (IllegalAccessException | InvocationTargetException
						| NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		};
		return check;
	}

	public Set<String> getCheckedIds() {
		return checkedIds;
	}

}
